package com.yswy.backstage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.backstage.service.PackageVersionService;
import com.yswy.common.enums.TfPlatformEnums;
import com.yswy.common.enums.XtPlatformEnums;
import com.yswy.common.enums.base.TypeEnum;
import com.yswy.common.utils.HuaWeiYunOBSUtil;
import com.yswy.common.utils.StringUtils;
import com.yswy.domain.common.BasePojo;
import com.yswy.domain.entity.AppProject;
import com.yswy.domain.entity.PackageManageLog;
import com.yswy.domain.entity.PackageVersion;
import com.yswy.domain.entity.user.User;
import com.yswy.domain.framework.context.CurrentUserContext;
import com.yswy.domain.mapper.PackageVersionMapper;
import com.yswy.domain.vo.gld.addOrUpate.PackageVersionAddOrUpdate;
import com.yswy.domain.vo.gld.search.PackageVersionSearch;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

import static com.yswy.backstage.utils.CrudUtils.selectCount;
import static com.yswy.common.constants.WarnMsgConstants.NULL_MSG;


@Slf4j
@Service
public class PackageVersionServiceImpl extends ServiceImpl<PackageVersionMapper, PackageVersion>
        implements PackageVersionService {
    @Autowired
    private HuaWeiYunOBSUtil huaWeiYunOBSUtil;
    @Override
    public Page<PackageVersion> listPackageVersion(PackageVersionSearch packageVersionSearch) {
        if (StringUtils.isNotEmpty(packageVersionSearch.getXtPlatform())){
            packageVersionSearch.setXtPlatform(TypeEnum.messageOf(XtPlatformEnums.class , packageVersionSearch.getXtPlatform()).toString());
        }
        Page<PackageVersion> packageVersions = baseMapper.pagePackageVersion(packageVersionSearch
                , new Page<>(packageVersionSearch.getPageNum() , packageVersionSearch.getPageSize()) );
        for (PackageVersion packageVersion:packageVersions.getRecords()){
            TypeEnum typeEnum = TypeEnum.typeEnumOf(XtPlatformEnums.class , packageVersion.getXtPlatform());
            packageVersion.setXtPlatform(typeEnum.getMessage());
        }

        return packageVersions;
    }

    @Override
    public void addOrUpdatePackageVersion(PackageVersionAddOrUpdate res) {
        User user = CurrentUserContext.getCurrentUser();

        if (res.getPackagefileUrl() != null ){
            String fileUrl = huaWeiYunOBSUtil.toUrl(res.getPackagefileUrl() , "datePlatform/PackageVersion");
            res.setFileUrl(fileUrl);
        }
        Boolean isExistVersion =  isExistVersion(res.getProjectId(), res.getVersionCode());

        if (res.getId() != null){
            //更新数据
            PackageVersion bo = getById(res.getId());
            Assert.state(null != bo && null != bo.getId(), NULL_MSG);
            BeanUtil.copyProperties(res, bo);
            BasePojo.setBase(user.getId().longValue(), false, bo);
            updateById(bo);
        }else {
            if (isExistVersion){
                throw new IllegalStateException("版本已存在");
            }
            PackageVersion packageVersion = new PackageVersion(res , user);
            save(packageVersion);
        }
    }

    @Override
    public void updateisRelease(Long packageVersionId, Integer isRelease) {
        User user = CurrentUserContext.getCurrentUser();
        Integer count = selectCount(baseMapper, s -> s.eq(BasePojo::getId, packageVersionId));
        Assert.state(null != count && 0 < count, "未找到指定信息，请刷新重试");
        PackageVersion bo = getById(packageVersionId);
        BasePojo.setBase(user.getId().longValue(), false, bo);
        bo.setIsRelease(isRelease);
        updateById(bo);
    }

    @Override
    public Boolean isExistVersion(Long projectId, String versionCode) {
        Integer count = count(new LambdaQueryWrapper<PackageVersion>()
                .eq(PackageVersion::getVersionCode , versionCode)
                .eq(PackageVersion::getProjectId , projectId)
                .eq(BasePojo::getIsDel , 0));
        if (count>0){
            return true ;
        }

        return false;
    }

    @Override
    public PackageVersion getMaxVersion(Long projectId) {
        return getOne(new LambdaQueryWrapper<PackageVersion>()
                .eq(PackageVersion::getProjectId , projectId)
                .eq(BasePojo::getIsDel , 0)
                .eq(PackageVersion::getIsRelease ,1 )
                .orderByDesc(PackageVersion::getVersion)
                .last("limit 1"));
    }

    @Override
    public List<PackageVersion> getVersionByProjectId(Long projectId) {
        List<PackageVersion> packageVersions = list(new LambdaQueryWrapper<PackageVersion>().eq(PackageVersion::getProjectId ,projectId)
                .eq(BasePojo::getIsDel , 0)
                .eq(PackageVersion::getIsRelease ,1 )
                .orderByDesc(PackageVersion::getVersion));
        for (PackageVersion packageVersion:packageVersions){
            TypeEnum typeEnum = TypeEnum.typeEnumOf(XtPlatformEnums.class , packageVersion.getXtPlatform());
            packageVersion.setXtPlatform(typeEnum.getMessage());
        }

        return packageVersions;
    }

    @Override
    public void delPackageVersion(Long packageVersionId) {
        User user = CurrentUserContext.getCurrentUser();
        Integer count = selectCount(baseMapper, s -> s.eq(BasePojo::getId, packageVersionId));
        Assert.state(null != count && 0 < count, "未找到指定信息，请刷新重试");
        PackageVersion bo = new PackageVersion();
        bo.setId(packageVersionId);
        bo.setIsDel(1);
        BasePojo.setBase(user.getId().longValue(), false, bo);
        updateById(bo);
    }
}
